﻿#ifndef CAMERA_H
#define CAMERA_H

#include <QWidget>
#include <QCamera>
#include <QCameraInfo>
#include <opencv2/opencv.hpp>
#include <QMessageBox>
#include <QFile>
#include <QFileInfo>

#ifdef _WIN32
#include <Windows.h>
//#define DEFAULT_SAVING_PATH "C:/Users/admin/Desktop/"
#define SLEEP_FOR_SECONDS(seconds) Sleep(seconds * 1000)
#define SLEEP_FOR_MSECONDS(mseconds) Sleep(mseconds)
#else
#include <unistd.h>
//#define DEFAULT_SAVING_PATH "/tmp/"
#define SLEEP_FOR_SECONDS(seconds) usleep(seconds * 1000000)
#define SLEEP_FOR_MSECONDS(mseconds) usleep(mseconds * 1000)
#endif

//#ifdef __cplusplus
//extern "C"
//{
//#endif
//#include <libavcodec/avcodec.h>
//#include <libavformat/avformat.h>
//#include <libavutil/dict.h>
//#ifdef __cplusplus
//};
//#endif

//class QCamera;
//class QCameraInfo;

namespace Ui {
class Camera;
}

class Camera : public QWidget
{
    Q_OBJECT

public:
    explicit Camera(QWidget *parent = nullptr);
    ~Camera();

private:
    Ui::Camera *ui;

private slots:
    void on_comboBox_way_activated(const QString &arg1);
    void on_pushButton_test_clicked();

    void on_pushButton_save_clicked();

    void on_pushButton_select_clicked();

public:
    QMessageBox msgBox_err;
    QMessageBox msgBox_inf;
    QCamera m_camera;                   //当前摄像头(用于UI中显示可用摄像头,获取设备序号)
    cv::VideoCapture capture;           //摄像头(实际使用的摄像头类型)
    QCameraInfo m_camera_info;
    QList<QCameraInfo> m_cameras;       //可用本地摄像头列表
    bool cameraIsOpen;                  //摄像机是否打开标志
    std::string cachePath;              //录像缓存路径
    int videoStopTag;
    cv::VideoWriter videoWriter;
    QString settingsFilePath;


    /**
     * @brief: 获取摄像头支持的分辨率，输入到combox列表
    */
    //void getResolution(QCameraInfo &c_info);

    /**
     * @brief: 排列可用摄像头设备
    */
    void populateCameraList();

    /**
     * @brief: 初始化摄像头设备
    */
    int videoCaptureInit();

    /**
     * @brief: 识别图像颜色
     * @param img:	传入图片
     * @param lower:	色域底
     * @param upper:	色域顶
     * @return: true --- 识别到颜色
     *			false --- 未识别颜色
    */
    bool colorRecog(cv::Mat img, cv::Scalar lower, cv::Scalar upper);

    /**
     * @brief: 测试是否进入桌面
     * @param stoparg:	停止标志位,置0则退出videoBegin()循环
     * @return: true/false
    */
    bool isEnterDesktop(cv::VideoCapture& capture);

    /**
     * @brief: 裁剪图片中间部分
     * @param coefficient: 裁剪系数
     * @return:裁剪后的图片
    */
    cv::Mat cropImageCenter(const cv::Mat& src, float coefficient);

    /**
     * @brief: 根据系数绘制矩形
     * @param coefficient: 系数(矩形编程/图片边长)
     * @return:画线后的图片
    */
    cv::Mat drawRec(const cv::Mat& src, float coefficient);

    /**
     * @brief: 删除缓存视频
    */
    bool cacheDelete();

    /**
     * @brief: 保存缓存视频
    */
    bool cacheSaving(const std::string filename = "");

signals:
    void signal_videoBegin();
    void signal_videoEnd();
    void signal_cameraOpen();
    void signal_cameraReady();

public slots:
    void slot_videoBegin(void);
    void slot_videoStop(void);
    void slot_cameraOpen(void);
    void slot_cameraReady(void);
};

#endif // CAMERA_H
